package svc

import (
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"github.com/zeromicro/go-zero/zrpc"
	"navi-mumbai/app/api/admin/internal/auth"
	"navi-mumbai/app/api/admin/internal/config"
	"navi-mumbai/app/api/admin/internal/model/pg"
	gameAdmin "navi-mumbai/app/rpc/game/client/gameadmin"
	"navi-mumbai/app/rpc/pay/client/payuser"
	"navi-mumbai/app/rpc/stat/client/statadmin"
	ucAdmin "navi-mumbai/app/rpc/usercenter/client/ucadmin"
	"navi-mumbai/app/rpc/usercenter/client/ucsys"
	ucUsers "navi-mumbai/app/rpc/usercenter/client/ucusers"
	walletAdmin "navi-mumbai/app/rpc/wallet/client/walletadmin"
	"navi-mumbai/app/rpc/wallet/client/walletsys"
	walletUsers "navi-mumbai/app/rpc/wallet/client/walletusers"
)

type ServiceContext struct {
	Config config.Config

	GameAdminRpc   gameAdmin.GameAdmin
	UcUsersRpc     ucUsers.UcUsers
	UcSysRpc       ucsys.UcSys
	UcAdminRpc     ucAdmin.UcAdmin
	WalletUsersRpc walletUsers.WalletUsers
	WalletAdminRpc walletAdmin.WalletAdmin
	PayUserRpc     payuser.PayUser
	WalletSysRpc   walletsys.WalletSys
	StatAdminRpc   statadmin.StatAdmin

	ApiLogModel   pg.ApiLogModel
	LoginLogModel pg.LoginLogModel
	ManagerModel  pg.ManagerModel

	Auth *auth.ApiPermission
}

func NewServiceContext(c config.Config) *ServiceContext {
	sqlConn := sqlx.NewMysql(c.DB.DataSource) //sqlx.NewSqlConn(dbkey.PostgresDriverName, c.DB.DataSource)
	svc := &ServiceContext{
		Config:         c,
		GameAdminRpc:   gameAdmin.NewGameAdmin(zrpc.MustNewClient(c.GameRpcConf)),
		UcUsersRpc:     ucUsers.NewUcUsers(zrpc.MustNewClient(c.UsercenterRpcConf)),
		UcAdminRpc:     ucAdmin.NewUcAdmin(zrpc.MustNewClient(c.UsercenterRpcConf)),
		UcSysRpc:       ucsys.NewUcSys(zrpc.MustNewClient(c.UsercenterRpcConf)),
		WalletUsersRpc: walletUsers.NewWalletUsers(zrpc.MustNewClient(c.WalletRpcConf)),
		WalletAdminRpc: walletAdmin.NewWalletAdmin(zrpc.MustNewClient(c.WalletRpcConf)),
		WalletSysRpc:   walletsys.NewWalletSys(zrpc.MustNewClient(c.WalletRpcConf)),
		PayUserRpc:     payuser.NewPayUser(zrpc.MustNewClient(c.PayRpcConf)),
		StatAdminRpc:   statadmin.NewStatAdmin(zrpc.MustNewClient(c.StatRpcConf)),

		ApiLogModel:   pg.NewApiLogModel(sqlConn, c.Cache),
		LoginLogModel: pg.NewLoginLogModel(sqlConn, c.Cache),
		ManagerModel:  pg.NewManagerModel(sqlConn, c.Cache),
	}
	svc.Auth = auth.InitAdminPermission(svc.ManagerModel)
	return svc
}
